3.4.11 Collect

Collect simplifies the task of factoring similar terms in a large expression by gathering them to one side. The subject provides a template that is matched against additive and multiplicative operands. An operand that contains a matching subexpression is commuted to the left.

For example, x^3-1 transforms to x^3-2⋅x^2+x-x^2+2⋅x-1 after distribution and simplification. Them terms in x^2 are commuted to the left by selecting x^3-2⋅.{x^2}+x-x^2+2⋅x-1 or x^3-2⋅x^2+x-.{x^2}+2⋅x-1 and Collect . With either subject, the result is -(2⋅x^2)-x^2+x^3+x+2⋅x-1. Then the first two terms can be selected, factored and simplified to -3⋅x^2+x^3+x+2⋅x-1.

Similarly, x^4-2⋅x^2+1 factors to (x-1)⋅(x+1)⋅(x-1)⋅(x+1). With the selection (.{x-1})⋅(x+1)⋅(x-1)⋅(x+1), Collect produces (x-1)⋅(x-1)⋅(x+1)⋅(x+1) which can be factored and simplified to (x-1)^2⋅(x+1)^2.